﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Configuration;
using System.Data;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Remoting;


namespace IntelligentCMS
{
    public class CMSMain
    {


        public static Panel RenderPage(long PageID)
        {
            
            Dictionary<string, string> item = new Dictionary<string, string>();

            item.Add("title", "Violent Acres");
            item.Add("link", "http://www.violentacres.com");
            item.Add("description", "Like You, But With Poor Impulse Control");
            item.Add("copyright", "Copyright 2005 Violent Acres");
            item.Add("ttl", "300");

            Dictionary<string, string> childitem1 = new Dictionary<string, string>();

            childitem1.Add("title", "Violent Acres 234234234");
            childitem1.Add("link", "http://www.violentacres.com/Top");
            childitem1.Add("description", "Like You, But With Poor Impulse Control");
            childitem1.Add("pubDate", "5/20/2011");
            childitem1.Add("author", "Bob");
            childitem1.Add("category", "Emo");


            Dictionary<string, string> childitem2 = new Dictionary<string, string>();

            childitem2.Add("title", "Drexel");
            childitem2.Add("link", "http://www.drexel.com");
            childitem2.Add("description", "Join us! It doesn't hurt anymore.");
            childitem2.Add("pubDate", "5/18/2011");
            childitem2.Add("author", "James");
            childitem2.Add("category", "Emo");

            //TestField parent = new TestField(item);
            //TestField child1 = new TestField(childitem1);
            //TestField child2 = new TestField(childitem2);


            List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
            list.Add(childitem1);
            list.Add(childitem2);


            //FileInfo newInfo = new FileInfo("C:\\Users\\Ryan\\Documents\\Visual Studio 2008\\WebSites\\IntelligentCMS\\plugins\\RSSplugin.dll");
            FileInfo newInfo = new FileInfo(ConfigurationSettings.AppSettings["serverpath"] + "plugins\\RSSplugin.dll");

            Assembly theAssembly = Assembly.LoadFile(newInfo.FullName);
            Type pluginType = theAssembly.GetType("RSSPlugin.RSSPlugin");
            object newPage = Activator.CreateInstance(pluginType);

            PropertyInfo Fields = pluginType.GetProperty("Fields");
            Fields.SetValue(newPage, item, null);

            PropertyInfo ChildFields = pluginType.GetProperty("ChildFields");
            ChildFields.SetValue(newPage, list, null);

            //newPage.Fields = parent;
            //newPage.ChildFields = list;
            //newPage.CreatePage();

            Panel genuine = (Panel)pluginType.InvokeMember("CreateContent", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, newPage, null);
            return genuine;
            //System.Diagnostics.Debug.Write(genuine);



        }






        public static Panel RenderPage2(long PageID)
        {

            Dictionary<string, string> item = new Dictionary<string, string>();

            item.Add("title", "My Comic Book");
            item.Add("description", "A collection of my favorite xkcd comics!");

            Dictionary<string, string> childitem1 = new Dictionary<string, string>();
            Dictionary<string, string> childitem2 = new Dictionary<string, string>();
            Dictionary<string, string> childitem3 = new Dictionary<string, string>();
            Dictionary<string, string> childitem4 = new Dictionary<string, string>();
            Dictionary<string, string> childitem5 = new Dictionary<string, string>();
            Dictionary<string, string> childitem6 = new Dictionary<string, string>();
            Dictionary<string, string> childitem7 = new Dictionary<string, string>();
            Dictionary<string, string> childitem8 = new Dictionary<string, string>();
            Dictionary<string, string> childitem9 = new Dictionary<string, string>();
            Dictionary<string, string> childitem10 = new Dictionary<string, string>();

            childitem1.Add("title", "Balloon");
            childitem1.Add("image", ConfigurationSettings.AppSettings["serverpath"] + "plugins\\ComicBookPlugin\\balloon.png");
            childitem1.Add("pubDate", "5/20/2011");

            childitem2.Add("title", "Barrel");
            childitem2.Add("image", ConfigurationSettings.AppSettings["serverpath"] + "plugins\\ComicBookPlugin\\barrel_cropped_(1).jpg");
            childitem2.Add("pubDate", "5/18/2011");

            childitem3.Add("title", "Marketing Interview");
            childitem3.Add("image", ConfigurationSettings.AppSettings["serverpath"] + "plugins\\ComicBookPlugin\\marketing_interview.png");
            childitem3.Add("pubDate", "5/18/2011");

            childitem4.Add("title", "Meerkat");
            childitem4.Add("image", ConfigurationSettings.AppSettings["serverpath"] + "plugins\\ComicBookPlugin\\meerkat.jpg");
            childitem4.Add("pubDate", "5/17/2011");

            childitem5.Add("title", "Pi");
            childitem5.Add("image", ConfigurationSettings.AppSettings["serverpath"] + "plugins\\ComicBookPlugin\\pi.jpg");
            childitem5.Add("pubDate", "5/14/2011");

            childitem6.Add("title", "Pillow Talk");
            childitem6.Add("image", ConfigurationSettings.AppSettings["serverpath"] + "plugins\\ComicBookPlugin\\pillow_talk.jpg");
            childitem6.Add("pubDate", "5/14/2011");

            childitem7.Add("title", "Pointers");
            childitem7.Add("image", ConfigurationSettings.AppSettings["serverpath"] + "plugins\\ComicBookPlugin\\pointers.png");
            childitem7.Add("pubDate", "5/14/2011");

            childitem8.Add("title", "Sandwich");
            childitem8.Add("image", ConfigurationSettings.AppSettings["serverpath"] + "plugins\\ComicBookPlugin\\sandwich.png");
            childitem8.Add("pubDate", "5/12/2011");

            childitem9.Add("title", "Su Doku");
            childitem9.Add("image", ConfigurationSettings.AppSettings["serverpath"] + "plugins\\ComicBookPlugin\\su_doku.jpg");
            childitem9.Add("pubDate", "4/26/2011");

            childitem10.Add("title", "Velociraptors");
            childitem10.Add("image", ConfigurationSettings.AppSettings["serverpath"] + "plugins\\ComicBookPlugin\\velociraptors.jpg");
            childitem10.Add("pubDate", "4/12/2011");

            //TestField parent = new TestField(item);
            //TestField child1 = new TestField(childitem1);
            //TestField child2 = new TestField(childitem2);


            List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
            list.Add(childitem1);
            list.Add(childitem2);


            //FileInfo newInfo = new FileInfo("C:\\Users\\Ryan\\Documents\\Visual Studio 2008\\WebSites\\IntelligentCMS\\plugins\\RSSplugin.dll");
            FileInfo newInfo = new FileInfo(ConfigurationSettings.AppSettings["serverpath"] + "plugins\\ComicBookPlugin.dll");
            //dll might not work because I'm not sure I've compiled it correctly

            Assembly theAssembly = Assembly.LoadFile(newInfo.FullName);
            Type pluginType = theAssembly.GetType("ComicBookPlugin.ComicBookPlugin");
            object newPage = Activator.CreateInstance(pluginType);

            PropertyInfo Fields = pluginType.GetProperty("Fields");
            Fields.SetValue(newPage, item, null);

            PropertyInfo ChildFields = pluginType.GetProperty("ChildFields");
            ChildFields.SetValue(newPage, list, null);

            //newPage.Fields = parent;
            //newPage.ChildFields = list;
            //newPage.CreatePage();

            Panel genuine = (Panel)pluginType.InvokeMember("CreateContent", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, newPage, null);
            return genuine;
            //System.Diagnostics.Debug.Write(genuine);



        }







        public static Panel RenderControl(long ControlID)
        {

            Panel newPanel = new Panel();


            List<Control> theControls = new List<Control>();

            /*
            CMSDbDataLayer control = new CMSDbDataLayer(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString, CMSDbDataHelper.Microsoft);
            control.AddQuery("SELECT Field.[Name], FieldType.[Name] FROM Field INNER JOIN ContentTypeFields ON Field.ID = ContentTypeFields.FieldID WHERE ContentTypeFields.ContentTypeID = " + id);
            control.SelectQuery = true;
            control.Execute();
            */


            string filename = string.Empty;

            

            TextReader newReader = File.OpenText(filename);

            string filecontents = newReader.ReadToEnd();

            newReader.Close();

            List<string> reservedTags = CMSDataLayer.GetAllTags();

            CMSTagParser ctp = new CMSTagParser(reservedTags);

            List<CMSTag> tags = ctp.ParseTags(filecontents);





            //List<string> tags = new List<string>();

            DataTable fields = CMSDataLayer.GetAllFields();



            foreach (Object tag in tags)
            {

               

                if(tag == "String") {
                
                
                
                }
                else if (tag == "Integer") { 
                
                
                }
                else if (tag == "File") { 
                
                
                }


                else
                {
                    bool literal = true;

                    foreach(DataRow row in fields.Rows)
                    {
                        if (tag == row["Tag"])
                        {
                            //string filename = CMSDataLayer.GetFilenameFromField(tag).Rows[0]["Filename"].ToString();
                           

                           
                            literal = false;
                            break;

                        }

                        


                    }


                    if (literal)
                    {
                            Literal newLiteral = new Literal();
                            //newLiteral.Text = tag;

                            theControls.Add(newLiteral);

                    }


                }

            }


            foreach(Control c in theControls) {

                newPanel.Controls.Add(c);


            }

            

            return newPanel;


        }
    }
}
